package com.scuec.groupfive.entity;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.List;

@Data
public class ExamQuestion {
    private Long id;
    private Long examId;
    private Long questionId;
    private String userAnswer;
    private Boolean isCorrect; // 已改为基本类型，不能为null
    private Double score;
    private Integer sequence;
    private String questionType;

    // 关联的题目详情
    private QuestionInfo questionInfo;
    private ChoiceQuestion choiceQuestion;
    private FillBlankQuestion fillBlankQuestion;
    private SubjectiveQuestion subjectiveQuestion;

    // 获取正确答案
    public String getCorrectAnswer() {
        if (questionType.startsWith("single_choice") || questionType.startsWith("multiple_choice")) {
            return choiceQuestion != null ? choiceQuestion.getAnswer() : "";
        } else if (questionType.equals("fill_in_the_blank")) {
            if (fillBlankQuestion == null) {
                return "";
            }
            String blanksJson = fillBlankQuestion.getBlanks(); // 获取 JSON 字符串
            if (blanksJson != null && !blanksJson.isEmpty()) {
                try {
                    List<String> blanksList = JSON.parseArray(blanksJson, String.class); // 解析 JSON
                    return blanksList.toString(); // 或者直接使用 blanksList
                } catch (Exception e) {
                    e.printStackTrace();
                    return ""; // 解析失败返回空字符串
                }
            }
            return "";
        } else if (questionType.equals("subjective")) {
            return subjectiveQuestion != null ? subjectiveQuestion.getReferenceAnswer() : "";
        }
        return "";
    }

    // 获取题目解析
    public String getQuestionAnalysis() {
        if (questionType.startsWith("single_choice") || questionType.startsWith("multiple_choice")) {
            return choiceQuestion != null ? choiceQuestion.getAnalysis() : "";
        } else if (questionType.equals("fill_in_the_blank")) {
            return fillBlankQuestion != null ? fillBlankQuestion.getAnalysis() : "";
        } else if (questionType.equals("subjective")) {
            return subjectiveQuestion != null ? subjectiveQuestion.getAnalysis() : "";
        }
        return "";
    }

    public void setChoiceQuestion(ChoiceQuestion choiceQuestion) {
        this.choiceQuestion = choiceQuestion;
    }
}